package com.ppt.android.common.helper

import com.ppt.android.BuildConfig
import com.ppt.android.base.constant.VersionStatus
import com.zhy.http.okhttp.OkHttpUtils
import com.zhy.http.okhttp.callback.Callback
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import java.io.File
import java.util.concurrent.TimeUnit

/**
 *
 * @author ZouJiaCai
 * @since 2023/4/10 11:17
 */
object UploadHelper {

    private fun providerOkHttp(): OkHttpClient {
        // 日志拦截器部分
        val level = if (BuildConfig.VERSION_TYPE != VersionStatus.RELEASE) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
        val logInterceptor = HttpLoggingInterceptor().setLevel(level)

        return OkHttpClient.Builder()
            .connectTimeout(30L * 1000L, TimeUnit.MILLISECONDS)
            .readTimeout(30L * 1000L, TimeUnit.MILLISECONDS)
            .addInterceptor(logInterceptor)
            .retryOnConnectionFailure(true)
            .build()
    }

    fun initClient(){
        OkHttpUtils.initClient(providerOkHttp())
    }

    private fun uploadFile(url: String ,name: String,file: File,callBack: Callback<Any>){
        OkHttpUtils.post()
            .url(url)
            .addFile(name,file.name,file)
            .build()
            .execute(callBack)
    }

}